//
//  ViewController.swift
//  StockProject
//
//  Created by mc on 2017/12/20.
//  Copyright © 2017年 zhangyufei. All rights reserved.
//

import UIKit
import Alamofire
import Instructions
import TSExtension

class ViewController: UIViewController, CoachMarksControllerDelegate, CoachMarksControllerDataSource {
    
    let coachMarks = CoachMarksController()
    let rightBtn = UIButton(type: .custom)
    let skip = JXMarkSkipView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.coachMarks.dataSource = self
        
        rightBtn.title = "引导"
        rightBtn.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
        rightBtn.titleColor = UIColor.red
        rightBtn.addTarget(self, action: #selector(action), for: .touchUpInside)
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: rightBtn)
        
//        coachMarks.overlay.allowTap = true //点击空白下一步
        coachMarks.overlay.color = UIColor.black.withAlphaComponent(0.5)
        
        
//        skip.title = "我知道了"
//        skip.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
        coachMarks.skipView = skip
        

        // 这里是分支2的代码
        view.backgroundColor = UIColor.yellow

    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        coachMarks.start(on: self)
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        coachMarks.stop(immediately: true)
    }
    @objc func action() {
        coachMarks.flow.showNext()
        print(1)
    }



    func numberOfCoachMarks(for coachMarksController: CoachMarksController) -> Int {
        return 2
    }
    func coachMarksController(_ coachMarksController: CoachMarksController, coachMarkAt index: Int) -> CoachMark {
        switch index {
        case 0:
            var mark = coachMarksController.helper.makeCoachMark(for: rightBtn) {
                (frame: CGRect) -> UIBezierPath in
                // This will create an oval cutout a bit larger than the view.
                return UIBezierPath(roundedRect: frame.insetBy(dx: -12, dy: 2), cornerRadius: 5)
            }
            mark.allowTouchInsideCutoutPath = true
            return mark
        case 1:
            let tab = UIViewController.root as! UITabBarController
            var markView: UIView? = nil
            for subview in tab.tabBar.subviews {
                if subview.isUserInteractionEnabled {
                    markView = subview
                    break
                }
            }
            return coachMarksController.helper.makeCoachMark(for: markView) {
                (frame: CGRect) -> UIBezierPath in
                // This will create an oval cutout a bit larger than the view.
                return UIBezierPath(ovalIn: frame.insetBy(dx: 10, dy: -10))
            }
        default:
            return coachMarksController.helper.makeCoachMark()
        }
        
        
    }
    func coachMarksController(_ coachMarksController: CoachMarksController, coachMarkViewsAt index: Int, madeFrom coachMark: CoachMark) -> (bodyView: CoachMarkBodyView, arrowView: CoachMarkArrowView?) {
//        let coachViews = coachMarksController.helper.makeDefaultCoachViews(withArrow: true, arrowOrientation: coachMark.arrowOrientation)
//        
//        coachViews.bodyView.hintLabel.text = "Hello! I'm a Coach Mark!"
//        coachViews.bodyView.nextLabel.text = "Ok!"
        
        let bodyView = JXMarkBodyView()
//        bodyView.hintLabel.text = "Hello! I'm a Coach Mark! \nI'm a Coach Mark!"
        
        let string = NSMutableAttributedString(string: "立即支付（需支付000元）", attributes: [.font: UIFont.size(13), .foregroundColor: UIColor.white])
        string.addAttributes([.font: UIFont.size(18)], range: NSMakeRange(0, 4))
        bodyView.hintLabel.attributedText = string
        
        var arrowView: JXMarkArrowView? = nil
        if let orientation = coachMark.arrowOrientation {
            arrowView = JXMarkArrowView(orientation: orientation)
        }

        return (bodyView: bodyView, arrowView: arrowView)
    }
    
    func coachMarksController(_ coachMarksController: CoachMarksController, constraintsForSkipView skipView: UIView, inParent parentView: UIView) -> [NSLayoutConstraint]? {
        var constraints = [NSLayoutConstraint]()
        constraints.append(NSLayoutConstraint(item: skip, attribute: .centerX, relatedBy: .equal, toItem: skip.superview, attribute: .centerX, multiplier: 1, constant: 0))
        constraints.append(NSLayoutConstraint(item: skip, attribute: .centerY, relatedBy: .equal, toItem: skip.superview, attribute: .centerY, multiplier: 1, constant: 0))
        return constraints
    }
}

